home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tlist.cc < prev    next >
C/C++ Source or Header  |  1993-05-21  |  3KB  |  130 lines

  1. /*
  2.  test/demo of generic lists
  3. */
  4.  
  5. #include <assert.h>
  6.  
  7. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  8.                        else _assert(#ex, __FILE__,__LINE__); }
  9.  
  10. #include <iostream.h>
  11. #include "iList.h"
  12.  
  13. int int_compare(int a, int b)
  14. {
  15.   return a - b;
  16. }
  17.  
  18. int inc(int x)
  19. {
  20.   return x + 1;
  21. }
  22.  
  23. int plus(int x, int y)
  24. {
  25.   return x + y;
  26. }
  27.  
  28. void printint(int x)
  29. {
  30.   cout << x << " ";
  31. }
  32.  
  33. void print(intList& l)
  34. {
  35.   l.apply(printint);
  36.   cout << "\n";
  37. }
  38.  
  39. int is_odd(int x)
  40. {
  41.   return x & 1;
  42. }
  43.  
  44. int is_even(int x)
  45. {
  46.   return (x & 1) == 0;
  47. }
  48.  
  49. intList sequence(int lo, int hi)
  50. {
  51.   if (lo > hi)
  52.     return intList();
  53.   else
  54.     return intList(lo, sequence(lo+1, hi));
  55. }
  56.  
  57. int old_rand = 9999;
  58.  
  59. int get_rand()
  60. {
  61.     old_rand = ((long)old_rand * (long)1243) % (long)971;
  62.     return old_rand;
  63. }
  64.  
  65. intList randseq(int n)
  66. {
  67.   if (n <= 0)
  68.     return intList();
  69.   int value = get_rand() % 50;
  70.   return intList(value, randseq(--n));
  71. }
  72.  
  73. main()
  74. {
  75.   intList a = sequence(1, 20);
  76.   cout << "\nintList a = sequence(1, 20);\n"; print(a);
  77.   assert(a.OK());
  78.   for (int i = 0; i < 20; ++i) assert(a[i] == i + 1);
  79.   assert(a.position(2) == 1);
  80.   intList b = randseq(20);
  81.   cout << "\nintList b = randseq(20);\n"; print(b);
  82.   intList c = concat(a, b);
  83.   cout << "\nintList c = concat(a, b);\n"; print(c);
  84.   assert(c.contains(a));
  85.   assert(c.contains(b));
  86.   assert(!(c.find(a).null()));
  87.   assert(c.find(b) == b);
  88.   intList d = map(inc, a);
  89.   for (i = 0; i < 20; ++i) assert(d[i] == a[i] + 1);
  90.   cout << "\nintList d = map(inc, a);\n"; print(d);
  91.   intList e = reverse(a);
  92.   cout << "\nintList e = reverse(a);\n"; print(e);
  93.   for (i = 0; i < 20; ++i) assert(e[i] == a[19 - i]);
  94.   intList f = select(is_odd, a);
  95.   cout << "\nintList f = select(is_odd, a);\n"; print(f);
  96.   intList ff = select(is_even, f);
  97.   assert(ff.null());
  98.   int  red = a.reduce(plus, 0);
  99.   cout << "\nint  red = a.reduce(plus, 0);\n"; cout << red;
  100.   int second = a[2];
  101.   cout << "\nint second = a[2];\n"; cout << second;
  102.   intList g = combine(plus, a, b);
  103.   cout << "\nintList g = combine(plus, a, b);\n"; print(g);
  104.   for (i = 0; i < 20; ++i) assert(g[i] == a[i] + b[i]);
  105.   g.del((intPredicate)is_odd);
  106.   cout << "\ng.del(is_odd);\n"; print(g);
  107.   ff = select(is_odd, g);
  108.   assert(ff.null());
  109.   b.sort(int_compare);
  110.   for (i = 1; i < 20; ++i) assert(b[i] >= b[i-1]);
  111.   cout << "\nb.sort(int_compare);\n"; print(b);
  112.   intList h = merge(a, b, int_compare);
  113.   cout << "\nintList h = merge(a, b, int_compare);\n"; print(h);
  114.   for (i = 1; i < 40; ++i) assert(h[i] >= h[i-1]);
  115.   for (Pix p = a.first(); p; a.next(p)) assert(h.contains(a(p)));
  116.   for (p = b.first(); p; b.next(p)) assert(h.contains(b(p)));
  117.   cout << "\nh via Pix:\n";
  118.   for (p = h.first(); p; h.next(p)) cout << h(p) << ", ";
  119.   cout << "\n";
  120.   assert(a.OK());
  121.   assert(b.OK());
  122.   assert(c.OK());
  123.   assert(d.OK());
  124.   assert(e.OK());
  125.   assert(f.OK());
  126.   assert(g.OK());
  127.   assert(h.OK());
  128.   cout << "\ndone\n";
  129. }
  130.